<?php

/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/3
 * Time: 下午6:47
 */
namespace App\Model\User;

use App\Model\Redis;
use App\Model\Model;
use EasySwoole\Config;

class UserModel extends Model {
	public function addUser(UserBean $user): string {
		$this->getModel ();
		$this->dbConnector ()->insert ( $this->prefix . 'user', $user->objToArray ( true ) );
		$this->free ();
		return $user->getUid ();
	}
	public function getUserByPhone($phone): UserBean {
		$this->getModel ();
		$userdata = $this->dbConnector ()->where ( "phone", $phone )->getone ( $this->prefix . 'user' );
		$this->free ();
		return (new UserBean ( $userdata ));
	}
	public function getUserByEmail($email): UserBean {
		$this->getModel ();
		$userdata = $this->dbConnector ()->where ( "email", $email )->getone ( $this->prefix . 'user' );
		$this->free ();
		return (new UserBean ( $userdata ));
	}
	public function getUserByUid($uid): UserBean {
		$this->getModel ();
		$userdata = $this->dbConnector ()->where ( "uid", $uid )->getone ( $this->prefix . 'user' );
		$this->free ();
		return (new UserBean ( $userdata ));
	}
	function getUserByToken(string $token = ""): UserBean {
		if (empty ( $token )) {
			return (new UserBean ());
		}
		$cache = new Redis ();
		$cache->getRedis ();
		$user = $cache->hGetAll ( $token );
		$cache->free ();
		if ($user) {
			return (new UserBean ( $user ));
		} else {
			return (new UserBean ());
		}
	}
	function updateUserbyToken($token, $field, $value) {
		if (empty ( $token )) {
			return false;
		}
		$cache = new Redis ();
		$cache->getRedis ();
		$re = $cache->hSet ( $token, $field, $value );
		$cache->free ();
		return $re;
	}
	function delUserFromCache(string $token) {
		$cache = new Redis ();
		$cache->getRedis ();
		$len = $cache->hLen ( $token );
		$dlen = $cache->hDelAll ( $token );
		$cache->free ();
		if ($dlen == $len) {
			return true;
		} else {
			return false;
		}
	}
	function addUserToCache(string $token, UserBean $user) {
		// 添加用户信息到缓存
		$cache = new Redis ();
		$cache->getRedis ();
		$cache->hMset ( $token, $user->toArray () );
		$cache->free ();
		return $token;
	}
	function addFdAndToken($fd, $token) {
		$cache = new Redis ();
		$cache->getRedis ();
		$cache->set ( $fd, $token );
		$cache->free ();
	}
	function getTokenByFd($fd) {
		$cache = new Redis ();
		$cache->getRedis ();
		$token = $cache->get ( $fd );
		$cache->free ();
		return $token;
	}
	function delTokenByFd($fd) {
		$cache = new Redis ();
		$cache->getRedis ();
		$token = $cache->del ( $fd );
		$cache->free ();
		return $token;
	}
	function addUidAndFd($uid, $fd) {
		$this->getModel ();
		$db = $this->dbConnector ();
		$db = $db->where ( "uid", $uid );
		$re = $db->update ( $this->prefix . 'user', [ 
				'fd' => $fd 
		] );
		$this->free ();
		return $re;
	}
	function getFdByUid($uid) {
		$this->getModel ();
		$db = $this->dbConnector ();
		$db = $db->where ( "uid", $uid );
		$re = $db->getOne ( $this->prefix . 'user', "fd" );
		$this->free ();
		return $re ['fd'];
	}
	function delFdByUid($uid) {
		$this->getModel ();
		$db = $this->dbConnector ();
		$db = $db->where ( "uid", $uid );
		$re = $db->update ( $this->prefix . 'user', [ 
				'fd' => - 1 
		] );
		$this->free ();
		return $re;
	}
	function onLineNumUp() {
		$key = Config::getInstance ()->getConf ( 'USER.ONLINENUMKEY' );
		// 添加用户信息到缓存
		$cache = new Redis ();
		$cache->getRedis ();
		$re = $cache->incr ( $key );
		$cache->free ();
		return $re;
	}
	function onLineNumDown() {
		$num = $this->getOnlineNum ();
		if (floatval ( $num ) > 1) {
			$key = Config::getInstance ()->getConf ( 'USER.ONLINENUMKEY' );
			// 添加用户信息到缓存
			$cache = new Redis ();
			$cache->getRedis ();
			$re = $cache->decr ( $key );
			$cache->free ();
			return $re;
		} else {
			return 1;
		}
	}
	function getOnlineNum() {
		$key = Config::getInstance ()->getConf ( 'USER.ONLINENUMKEY' );
		$cache = new Redis ();
		$cache->getRedis ();
		$num = $cache->get ( $key );
		$cache->free ();
		return $num;
	}
	function InitOnlineNum() {
		$key = Config::getInstance ()->getConf ( 'USER.ONLINENUMKEY' );
		$cache = new Redis ();
		$cache->getRedis ();
		$re = $cache->set ( $key, 1, 0 );
		$cache->free ();
		return $re;
	}
}